using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Krile.Forms.MainForm.Controls.XTab
{
    public class XTabButtonItem : XTabItemBase,IDisposable
    {
        private XTab parent;

        public object contains;

        private void Refresh()
        {
            if (SuspendRedraw) return;
            parent.Refresh();
        }

        public XTabButtonItem(XTab par, string n, Image icoImage)
            : base()
        {
            SuspendRedraw = false;
            parent = par;
            name = n;
            iconImage = icoImage;
        }

        private string name;
        /// <summary>
        /// visible name
        /// </summary>
        public override string Name
        {
            get { return name; }
            set { name = value; }
        }

        private Image iconImage;
        /// <summary>
        /// iconimage
        /// </summary>
        public Image IconImage
        {
            get { return iconImage; }
            set { iconImage = value; }
        }

        private Rectangle myRegionRect;

        /// <summary>
        /// client rectangle
        /// </summary>
        public override Rectangle ClientRectangle
        {
            get { return myRegionRect; }
        }


        private bool enabled = true;
        /// <summary>
        /// Enable this button
        /// </summary>
        public bool Enabled
        {
            get { return enabled; }
            set
            {
                enabled = value;
                Refresh();
            }
        }

        /// <summary>
        /// Suspend exec redraw
        /// </summary>
        public bool SuspendRedraw { get; set; }

        /// <summary>
        /// location
        /// </summary>
        public Point Location
        {
            get { return myRegionRect.Location; }
            set { myRegionRect.Location = value; }
        }

        /// <summary>
        /// location top
        /// </summary>
        public int Top
        {
            get { return myRegionRect.Y; }
            set { myRegionRect.Y = value; }
        }

        /// <summary>
        /// location left
        /// </summary>
        public int Left
        {
            get { return myRegionRect.X; }
            set { myRegionRect.X = value; }
        }

        bool pressing = false;
        bool hovering = false;
        public void Draw(Graphics g)
        {
            Rectangle innerRect = ClientRectangle;
            innerRect.Width--;
            innerRect.Height--;
            if (enabled && (hovering || clickLock))
            {
                if (pressing || clickLock)
                {
                    g.FillRectangle(Brushes.DeepSkyBlue, innerRect);
                    g.DrawRectangle(Pens.RoyalBlue, innerRect);
                }
                else
                {
                    g.FillRectangle(Brushes.SkyBlue, innerRect);
                    g.DrawRectangle(Pens.SteelBlue, innerRect);
                }
            }
            Point imgPoint = ClientRectangle.Location;
            imgPoint.X += ClientRectangle.Width / 2;
            imgPoint.Y += ClientRectangle.Height / 2;
            imgPoint.X -= iconImage.Width / 2;
            imgPoint.Y -= iconImage.Height / 2;
            if (enabled)
                g.DrawImage(iconImage, imgPoint);
            else
            {
                using (Image dImg = Krile.Imported.Snippets.Images.TransformGrayscale(iconImage))
                {
                    g.DrawImage(dImg, imgPoint);
                }
            }
        }

        /// <summary>
        /// refresh this size
        /// </summary>
        /// <param name="g"></param>
        public void UpdateMeasure(Graphics g)
        {
            this.myRegionRect.Size = new Size(XTab.buttonWidth, parent.MaxTabSize.Height - XTab.outerMargin * 2);
        }

        private bool clickLock = false;
        /// <summary>
        /// clicklock state
        /// </summary>
        public bool ClickLock
        {
            get { return clickLock; }
            set
            {
                clickLock = value;
                Refresh();
            }
        }

        public void Dispose()
        {
            if (this.iconImage != null)
            {
                iconImage.Dispose();
                iconImage = null;
            }
        }

        public override void OnMouseEnter(object obj, MouseEventArgs e)
        {
            if (!enabled) return;
            hovering = true;
            Refresh();
        }

        public override void OnMouseLeave(object obj, MouseEventArgs e)
        {
            hovering = false;
            if (!enabled) return;
            Refresh();
        }

        public override void OnMouseDown(object obj, MouseEventArgs e)
        {
            pressing = true;
            Refresh();
        }

        public override void OnMouseUp(object obj, MouseEventArgs e)
        {
            pressing = false;
            if (!enabled) return;
            Refresh();
        }

        public event MouseEventHandler Click;
        public override void OnMouseClick(object obj, MouseEventArgs e)
        {
            if (!enabled) return;
            if (Click != null)
                Click.Invoke(obj, e);
        }

        public override void OnMouseDoubleClick(object obj, MouseEventArgs e)
        {
            this.OnMouseClick(obj, e);
        }
    }
}
